#include "ParserTreeNode.h"
#include <iostream>

ParserTreeNode::ParserTreeNode()
	: leftChild(NULL), rightChild(NULL), atom(), treeType(NONE_TREE)
{
}

ParserTreeNode::ParserTreeNode(std::string str)
	: leftChild(NULL), rightChild(NULL), atom(str), treeType(NONE_TREE)
{
}

ParserTreeNode::ParserTreeNode(int number)
	: leftChild(0), rightChild(0), atom(number), treeType(NONE_TREE)
{
}


ParserTreeNode::~ParserTreeNode(void)
{
	if (leftChild)
		delete leftChild;
	if (rightChild)
		delete rightChild;
}


// add a left/right child whose atom is a number
ParserTreeNode* ParserTreeNode::addChild(bool leftOrRight, int number)
{
	ParserTreeNode* node = new ParserTreeNode(number);
	if (leftOrRight)
		this->leftChild = node;
	else
		this->rightChild = node;

	return node;
}

// add a left/right child whose atom is a literal
ParserTreeNode* ParserTreeNode::addChild(bool leftOrRight, std::string str)
{
	ParserTreeNode* node = new ParserTreeNode(str);
	if (leftOrRight)
		this->leftChild = node;
	else
		this->rightChild = node;

	return node;
}

// add a left/right child 
ParserTreeNode* ParserTreeNode::addChild(bool leftOrRight, ParserTreeNode* node)
{
	if (leftOrRight)
		this->leftChild = node;
	else
		this->rightChild = node;

	return node;
}


// get the left/right child
ParserTreeNode* ParserTreeNode::getChild(bool leftOrRight)
{
	if (leftOrRight)
		return this->leftChild;
	else
		return this->rightChild;
}

// copy a parser tree
ParserTreeNode* ParserTreeNode::copy()
{
	ParserTreeNode* node = NULL;
	if (this->isDOT())
		node = new ParserTreeNode();
	else if (this->isLiteral())
		node = new ParserTreeNode(this->atom.str);
	else
		node = new ParserTreeNode(this->atom.number);

	if (this->leftChild != NULL)
		node->leftChild = this->leftChild->copy();
	if (this->rightChild != NULL)
		node->rightChild = this->rightChild->copy();

	return node;
}